using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using OneSoft.EntityCore;
using OneSoft.DataAccess;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;

namespace BySoft.Server.DataAccess
{
	public partial class MdSupplierDA
	{
		#region GetByID

		/// <summary>
		/// 取得实体
		/// </summary>
		/// <param name="id">实体关键字</param>
		/// <returns></returns>
		public static MdSupplierEntity GetMdSupplierByID(String id)
		{
			string strSQL = "SELECT * FROM " + MdSupplierTable.C_TABLENAME;
			WhereBuilder wb = new WhereBuilder(strSQL);
			wb.AddAndCondition(MdSupplierTable.C_SUPPLIERID, SQLOperator.Equal, id);
			return OneSoft.DataAccess.DataAccessCommon.SelectSingle<MdSupplierEntity>(wb);
		}

		#endregion

		#region GetAll

		/// <summary>
		/// 获取所有数据
		/// </summary>
		/// <returns>所有数据集合</returns>
		public static DataSet GetMdSupplierAll()
		{
			string strSQL = "SELECT * FROM " + MdSupplierTable.C_TABLENAME;
			return OneSoft.DataAccess.DataAccessCommon.SelectDataSet(strSQL);
		}

		#endregion

		#region GetByCondition

		/// <summary>
		/// 根据Condition获取DS
		/// </summary>
		/// <returns>所有数据集合</returns>
		public static DataSet GetDSByCondition(string strCondition)
		{
			string strSQL = "SELECT * FROM " + MdSupplierTable.C_TABLENAME;
			WhereBuilder wb = new WhereBuilder(strSQL);
			wb.AddCondition(strCondition);
			return OneSoft.DataAccess.DataAccessCommon.SelectDataSet(wb);
		}

		/// <summary>
		/// 根据Condition获取实体集合
		/// </summary>
		/// <param name="strCondition">SQL条件</param>
		/// <returns>实体集合</returns>
		public static List<MdSupplierEntity> GetByCondition(string strCondition)
		{
			string strSQL = "SELECT * FROM " + MdSupplierTable.C_TABLENAME;
			WhereBuilder wb = new WhereBuilder(strSQL);
			wb.AddCondition(strCondition);
			return OneSoft.DataAccess.DataAccessCommon.Select<MdSupplierEntity>(wb);
		}

		#endregion


        private static string GetHeadSQL()
        {
            return @"SELECT A.*,a.SupplierID AS MatSupplierID, a.SupplierName AS MatSupplierName,c.ParameterName AS  SupplierScalerName
FROM MD_Supplier A 
LEFT JOIN SYS_Parameter c ON c.ParameterTypeID='SupplierScaler' AND c.ParameterCode=a.SupplierScaler
WHERE 1=1";
        }

        public static DataTable SelectList(string SupplierID, string SupplierName, string SupplierScaler, int pageIndex, int PageSize, out int m_count)
        {
            try
            {
                #region SQL Server
                string strcmd = GetHeadSQL();

                DataAccessParameterCollection dpc = new DataAccessParameterCollection();

                if (!string.IsNullOrEmpty(SupplierID))
                {
                    strcmd += " and A.SupplierID LIKE @SupplierID";
                    dpc.AddWithValue("@SupplierID", "%" + SupplierID + "%");
                }

                if (!string.IsNullOrEmpty(SupplierName))
                {
                    strcmd += " and A.SupplierName LIKE @SupplierName";
                    dpc.AddWithValue("@SupplierName", "%" + SupplierName + "%");
                }
                if (!string.IsNullOrEmpty(SupplierScaler))
                {
                    strcmd += " and A.SupplierScaler=@SupplierScaler";
                    dpc.AddWithValue("@SupplierScaler", SupplierScaler);
                }


                strcmd += " ORDER BY A.ModifyDate DESC";

                return DataAccessCommon.ExecuteDataTablePaging(strcmd, pageIndex, PageSize, dpc, out m_count);
                #endregion
            }
            catch
            {
                throw;
            }
        }

        public static bool CheckSupplierNameIsExists(string SupplierName, string SupplierID)
        {
            string strcmd = @"SELECT COUNT(SupplierID) FROM " + MdSupplierTable.C_TABLENAME + " WHERE 1=1 and SupplierName=@SupplierName and SupplierID!=@SupplierID";
            DataAccessParameterCollection dpc = new DataAccessParameterCollection();
            dpc.AddWithValue("@SupplierName", SupplierName);
            dpc.AddWithValue("@SupplierID", SupplierID);
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    object obj = broker.ExecuteSQLScalar(strcmd, dpc);
                    if (Convert.ToInt32(obj.ToString().Trim()) > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch
                {
                    throw;
                }
            }
        }

        public static DataTable SelectDialogList(string SupplierID, string SupplierName, string MaterialID, int pageIndex, int PageSize, out int m_count)
        {
            try
            {
                #region SQL Server
                string strcmd = @"select ms.*,s.SupplierName from MD_MaterialSupplier ms
join MD_Supplier s on s.SupplierID=ms.SupplierID where 1=1";

                DataAccessParameterCollection dpc = new DataAccessParameterCollection();

                if (!string.IsNullOrEmpty(SupplierID))
                {
                    strcmd += " and ms.SupplierID LIKE @SupplierID";
                    dpc.AddWithValue("@SupplierID", "%" + SupplierID + "%");
                }

                if (!string.IsNullOrEmpty(SupplierName))
                {
                    strcmd += " and s.SupplierName LIKE @SupplierName";
                    dpc.AddWithValue("@SupplierName", "%" + SupplierName + "%");
                }
                if (!string.IsNullOrEmpty(MaterialID))
                {
                    strcmd += " and ms.MaterialID=@MaterialID";
                    dpc.AddWithValue("@MaterialID", MaterialID);
                }


                strcmd += " ORDER BY ms.MaterialSupplierID asc";

                return DataAccessCommon.ExecuteDataTablePaging(strcmd, pageIndex, PageSize, dpc, out m_count);
                #endregion
            }
            catch
            {
                throw;
            }
        }

      
    }
}

